package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.BuildConfig;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.event.AlertLanguage;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.MainViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.update.AppDialogDownloadView;
import com.example.common.update.UpdateInfo;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

import static com.example.common.update.AppDialogDownloadView.isDisplayUpdateHint;

/**
 * 关于
 */
public class AboutActivity extends BaseActivity {

    @Bind(R.id.version_update_view) View mVersionUpdateView;
    @Bind(R.id.current_app_version_text) TextView mCurrentAppVersionText;
    @Bind(R.id.about_us_layout) LinearLayout mAboutUsLayout;
    @Bind(R.id.privacy_protection_layout) LinearLayout mPrivacyProtectionLayout;
    @Bind(R.id.version_update_layout)View mVersionUpdateLayout;
    @Bind(R.id.alert_language_layout) View mAlertLanguageLayout;

    private UpdateInfo mUpdateInfo;
    private MainViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, AboutActivity.class));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_about;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        checkVersionUpdate();

        ActivityBarHelper.init(this, R.string.title_about);

        mCurrentAppVersionText.setText(BuildConfig.VERSION_NAME);

        mAboutUsLayout.setOnClickListener(v -> WebViewActivity.aboutUs(this));

        mPrivacyProtectionLayout.setOnClickListener(v -> WebViewActivity.privacyProtection(this));

        mVersionUpdateLayout.setOnClickListener(v -> update());

        mAlertLanguageLayout.setOnClickListener(v -> alertLanguage());
    }

    private void alertLanguage() {
        Androids.LocalConfig.alterLanguage();
        String currentLanguage = Androids.LocalConfig.getLanguage();
        Androids.alertLanguage(this, currentLanguage);
        EventBus.getDefault().post(new AlertLanguage(currentLanguage));

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    private void checkVersionUpdate() {
        mViewModel.updateInfo().observe(this, updateInfo -> {
            this.mUpdateInfo = updateInfo;
            if (isDisplayUpdateHint(this, updateInfo.newVersion)){
                mVersionUpdateView.setVisibility(View.VISIBLE);
            }
        });
    }

    private void update(){
        if (mUpdateInfo != null){
            AppDialogDownloadView.show(this, mUpdateInfo);
        }
    }

}
